package com.marisfrolg.gateway.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

@Slf4j
public class JwtUtils {

    public static Claims verifyJwt(String token, String jwtKey) {
        //签名秘钥，和生成的签名的秘钥一模一样
        SecretKey key = generalKey(jwtKey);
        Claims claims;
        claims = Jwts.parser()  //得到DefaultJwtParser
                .setSigningKey(key)         //设置签名的秘钥
                .parseClaimsJws(token).getBody();
        return claims;
    }

    public static SecretKey generalKey(String key) {
        byte[] encodedKey = key.getBytes();
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }
}
